package com.cdzs.twocamera.util;

import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Created by M_genius on 2018/7/26.\
 * 描述:
 *  GPIO 高低电平控制器
 */

public class GPIOController {

    private static volatile GPIOController mInstance;
    private Process process;

    private GPIOController() {
    }

    /**
     * 单例获取 TCP 连接对象
     *
     * @return
     */
    public static GPIOController getInstance() {

        if (mInstance == null) {
            synchronized (GPIOController.class) {
                if (mInstance == null) {
                    mInstance = new GPIOController();
                }
            }
        }
        return mInstance;
    }

    /**
     * 打开GPIO口，设置高电平
     */
    public void open() {
        if (android.os.Build.PRODUCT.equals("rk3288_box")) {
            cmdGpioH();
        } else if (android.os.Build.PRODUCT.equals("rk3288")) {

        }
    }

    /**
     * 打开GPIO口，设置低电平
     */
    public void close(){
        if (android.os.Build.PRODUCT.equals("rk3288_box")) {
            cmdGpioL();
        } else if (android.os.Build.PRODUCT.equals("rk3288")) {

        }
    }

    //GPIO设置高：rk3288_box
    private void cmdGpioH() {
        // TODO Auto-generated method stub
        DataOutputStream dos = null;
        try {
            process = Runtime.getRuntime().exec("su");
            dos = new DataOutputStream(process.getOutputStream());
            dos.writeBytes("echo out > /sys/class/gpio/gpio256/direction" + "\n"); //设置引脚功能为输出
            dos.flush();
            dos.writeBytes("echo 1 > /sys/class/gpio/gpio256/value" + "\n");
            dos.flush();
            dos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //GPIO低：rk3288_box
    private void cmdGpioL() {
        // TODO Auto-generated method stub
        DataOutputStream dos = null;
        try {
            process = Runtime.getRuntime().exec("su");
            dos = new DataOutputStream(process.getOutputStream());
            dos.writeBytes("echo out > /sys/class/gpio/gpio256/direction" + "\n"); //设置引脚功能为输出
            dos.flush();
            dos.writeBytes("echo 0 > /sys/class/gpio/gpio256/value" + "\n");
            dos.flush();
            dos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
